package com.flyinzhen.customer.utils;

import com.alibaba.fastjson.JSONObject;
import com.flyinzhen.core.constants.ResponseStatus;
import com.flyinzhen.core.dto.TokenDto;
import com.flyinzhen.core.service.RedisService;
import com.flyinzhen.core.utils.CommonUtils;
import com.flyinzhen.core.utils.HttpUtils;
import com.flyinzhen.customer.constants.RedisCacheType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.CredentialsExpiredException;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @Title: ApplicationUtils.java
 * @Description: 系统通用工具类
 * @author yinzhen01@chinatopcredit.com
 * @date 2017年12月1日
 * @version V1.0
 */
@Component
public class ApplicationUtils {

	private static ApplicationUtils applicationUtils;

	@Value("${spring.application.name:DEFAULT}")
	private String applicationName;

	@Autowired
	private RedisService redisService;

	@PostConstruct
	public void init() {
		applicationUtils = this;
		applicationUtils.applicationName = this.applicationName;
		applicationUtils.redisService = this.redisService;
	}

    /**
     * 创建缓存主键
     */
    public static String cacheKey(String type, String key) {
    	return String.format("%s:%s:%s", applicationUtils.applicationName, type, key);
    }

	/**
	 * 从Redis获取Token键值
	 */
	public static String getToken() {
		String token = HttpUtils.getToken();
		if (StringUtils.isBlank(token)) {
			return null;
		}
		return CommonUtils.cacheKey(RedisCacheType.TOKEN.name(), token);
	}
}